package api.cie.rss.dal;

import java.util.EnumSet;
import java.util.Set;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import api.cie.rss.common.Category.Categories;
import api.cie.rss.common.ParameterKeys;

/**
 * The class to create or open the database
 * @author STL-API
 *
 */
public class RssReaderDatabaseHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "rssdata";
	private static final int DATABASE_VERSION = 1;
	
	// Database creation sql statement
	private static final String RSSTABLE_CREATE = 
		"create table RSS (_id integer primary key autoincrement, "
		+ "title text not null, url text not null, isActive integer not null);";
	private static final String CATEGORYTABLE_CREATE = 
		"create table Category (_id integer primary key autoincrement, "
		+ "title text not null);";
	private static final String RSS_CATEGORYTABLE_CREATE = 
		"create table RSS_Category (idRSS integer not null, "
		+ "idCategory integer not null, PRIMARY KEY(idRSS, idCategory));";
	
	private static final String ARTICLETABLE_CREATE = 
		"create table Article (_id integer primary key autoincrement, "
		+ "title text not null, description text not null, url text not null, "
		+ "idRSS integer not null, ratings integer not null, date text not null);";
	private static final String PARAMETERTABLE_CREATE = 
		"create table Parameter (_id integer primary key autoincrement, "
		+ "key text not null, value text not null);";
		
	/**
	 * for adding some default data in the database at the moment of creation
	 */
	private void insertDefaultData(SQLiteDatabase db) {
		/*
		 * insert categories
		 */
		Set<Categories> categories = EnumSet.allOf(Categories.class);
		for (int i=0; i<categories.toArray().length; i++) {
			Categories c = (Categories)categories.toArray()[i];
			ContentValues values = new ContentValues();
			values.put(CategoryDAL.KEY_ROWID, i+1);
			values.put(CategoryDAL.KEY_TITLE, c.toString());
			db.insert(CategoryDAL.DATABASE_TABLE, null, values);
		}
		/*
		 * insert parameters
		 */
		ContentValues values = new ContentValues();
		values.put(ParameterDAL.KEY_ROWID, 1);
		values.put(ParameterDAL.KEY_PARAMKEY, ParameterKeys.REFRESH_FREQUENCY);
		values.put(ParameterDAL.KEY_PARAMVALUE, ParameterKeys.FREQUENCY_10MIN);
		db.insert(ParameterDAL.DATABASE_TABLE, null, values);
		
		values = new ContentValues();
		values.put(ParameterDAL.KEY_ROWID, 2);
		values.put(ParameterDAL.KEY_PARAMKEY, ParameterKeys.FONT_SIZE);
		values.put(ParameterDAL.KEY_PARAMVALUE, ParameterKeys.FONT_NORMAL);
		db.insert(ParameterDAL.DATABASE_TABLE, null, values);
		
		values = new ContentValues();
		values.put(ParameterDAL.KEY_ROWID, 3);
		values.put(ParameterDAL.KEY_PARAMKEY, ParameterKeys.SORT_BY);
		values.put(ParameterDAL.KEY_PARAMVALUE, ParameterKeys.SORTBY_DATE);
		db.insert(ParameterDAL.DATABASE_TABLE, null, values);
	}
	
	public RssReaderDatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(RSSTABLE_CREATE);
		db.execSQL(ARTICLETABLE_CREATE);
		db.execSQL(CATEGORYTABLE_CREATE);
		db.execSQL(RSS_CATEGORYTABLE_CREATE);
		db.execSQL(PARAMETERTABLE_CREATE);		
		insertDefaultData(db);
	}

	// Method is called during an update of the database, e.g. if you increase
	// the database version
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(RssReaderDatabaseHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		/*
		 * Drop the existing tables when upgrading the database
		 */
		db.execSQL("DROP TABLE IF EXISTS RSS");
		db.execSQL("DROP TABLE IF EXISTS Category");
		db.execSQL("DROP TABLE IF EXISTS Article");
		db.execSQL("DROP TABLE IF EXISTS RSS_Category");
		db.execSQL("DROP TABLE IF EXISTS Parameter");
		onCreate(db);
	}

}
